﻿using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using System;

namespace MDCAdminTool.Controls
{
    public partial class ChartStatsBase : UserControl
    {
        #region DP Host
        public static readonly DependencyProperty HostProperty = 
            DependencyProperty.Register(
                "Host" ,
                typeof( CacheHost ) ,
                typeof( ChartStatsBase ) ,
                new UIPropertyMetadata( null , HostChange ) );

        public CacheHost Host
        {
            get { return (CacheHost)GetValue( HostProperty ); }
            set { SetValue( HostProperty , value ); }
        }

        private static void HostChange( DependencyObject d , DependencyPropertyChangedEventArgs e )
        {
            var cs = d as ChartStatsBase;
            if ( cs == null ) return;

            cs.Update();
        }
        #endregion

        #region DP CacheName
        public static readonly DependencyProperty CacheNameProperty = 
            DependencyProperty.Register(
               "CacheName" ,
               typeof( MdcCache ) ,
               typeof( ChartStatsBase ) ,
               new UIPropertyMetadata( null , CacheNameChange ) );

        public MdcCache CacheName
        {
            get { return (MdcCache)GetValue( CacheNameProperty ); }
            set { SetValue( CacheNameProperty , value ); }
        }

        private static void CacheNameChange( DependencyObject d , DependencyPropertyChangedEventArgs e )
        {
            var cs = d as ChartStatsBase;
            if ( cs == null ) return;

            cs.Update();
        }
        
        #endregion

        #region DP Title
        public static readonly DependencyProperty TitleProperty = 
            DependencyProperty.Register(
               "Title" ,
               typeof( string ) ,
               typeof( ChartStatsBase ) ,
               new UIPropertyMetadata( null , TitleChange ) );

        public string Title
        {
            get { return (string)GetValue( TitleProperty ); }
            set { SetValue( TitleProperty , value ); }
        }

        private static void TitleChange( DependencyObject d , DependencyPropertyChangedEventArgs e )
        {
            var cs = d as ChartStatsBase;
            if ( cs == null ) return;

            cs.Update();
        }

        #endregion

        #region DP SubTitle
        public static readonly DependencyProperty SubTitleProperty = 
            DependencyProperty.Register(
               "SubTitle" ,
               typeof( string ) ,
               typeof( ChartStatsBase ) ,
               new UIPropertyMetadata( null , SubTitleChange ) );

        public string SubTitle
        {
            get { return (string)GetValue( SubTitleProperty ); }
            set { SetValue( SubTitleProperty , value ); }
        }

        private static void SubTitleChange( DependencyObject d , DependencyPropertyChangedEventArgs e )
        {
            var cs = d as ChartStatsBase;
            if ( cs == null ) return;

            cs.Update();
        }

        #endregion

        protected ChartStatsBase()
        {
            Dispatcher.Invoke( 
                new Action( 
                    () =>
                        {
                            InitializeComponent();
                            DataContext = this;
                        } ) );
        }

        protected ChartStatsBase( CacheHost host ) : this()
        {
            Host = host;
        }
        protected ChartStatsBase( MdcCache cache ) : this()
        {
            CacheName = cache;
        }

        private void Update()
        {
            ChartData.ItemsSource = GetData();
        }

        protected virtual Collection<NameValue> GetData()
        {
            return new Collection<NameValue>();
        }

    }
}
